package com.dhx.dao.impl;

import com.dhx.dao.CompanyNeedDao;
import com.dhx.pojo.CompanyNeed;
import com.dhx.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CompanyNeedImpl implements CompanyNeedDao {
    @Override
    public int addInfo(CompanyNeed companyNeed) {
        String sql = "insert into t_companyneed values (?,?,?,?)";
        Object[] objs = {companyNeed.getCno(), companyNeed.getMajor(), companyNeed.getNeednum(), companyNeed.getDate()};
        return DBUtil.excuteDML(sql, objs);
    }

    @Override
    public int deleteInfoById(String no) {
        String sql = "delete from t_companyneed where cno= ? ";
        Object[] objs = {no};
        return DBUtil.excuteDML(sql, objs);
    }

    @Override
    public int updateInfo(CompanyNeed companyNeed) {
        String sql = "update t_companyneed set major=?,neednum=?,date=? where cno=? ";
        Object[] objs = {companyNeed.getMajor(), companyNeed.getNeednum(), companyNeed.getDate(), companyNeed.getCno()};
        return DBUtil.excuteDML(sql, objs);
    }

    @Override
    public CompanyNeed queryInfoById(String no) {
        Connection conn = DBUtil.getConnection();
        String sql = "select * from t_companyneed where cno=?";
        PreparedStatement ps = DBUtil.getPreparedStatement(conn, sql);
        try {
            ps.setString(1, no);
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        ResultSet rs = null;
        CompanyNeed companyNeed = null;
        try {
            rs = ps.executeQuery();
            if (rs.next()) {
                companyNeed = new CompanyNeed( rs.getString("cno"), rs.getString("major"), rs.getInt("neednum"), rs.getString("date"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(rs, ps, conn);
        }
        return companyNeed;
    }

    @Override
    public List<CompanyNeed> queryInfoes() {
        Connection conn = DBUtil.getConnection();
        String sql = "select * from t_companyneed";
        PreparedStatement ps = DBUtil.getPreparedStatement(conn, sql);
        ResultSet rs = null;
        List<CompanyNeed> list = new ArrayList<CompanyNeed>();
        try {
            rs = ps.executeQuery();
            while (rs.next()) {
                CompanyNeed companyNeed = new CompanyNeed( rs.getString("cno"), rs.getString("major"), rs.getInt("neednum"), rs.getString("date"));
                list.add(companyNeed);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(rs, ps, conn);
        }
        return list;
    }
}
